<?php
if(count(get_included_files()) == 1)
	die("Direct access not permitted.");

// include router
include_once( dirname(__FILE__) . '/vendor/router/Dispatcher.php');
include_once( dirname(__FILE__) . '/vendor/router/Route.php');
include_once( dirname(__FILE__) . '/vendor/router/Router.php');

// include form generator
include_once( dirname(__FILE__) . '/vendor/PFBC/Form.php');

// include Smarty
include_once( dirname(__FILE__) . '/classes/core/BlogSmarty.class.php');

// include Propel
include_once( dirname(__FILE__) . '/vendor/propel/runtime/lib/Propel.php' );

// Initialize Propel with the runtime configuration
Propel::init( dirname(__FILE__) . '/vendor/propel/build/conf/blogpropel-conf.php' );

// Add the generated 'classes' directory to the include path
set_include_path( dirname(__FILE__) . '/vendor/propel/build/classes' . PATH_SEPARATOR . get_include_path());

// include core
include_once( dirname(__FILE__) . '/classes/core/Authentication.php');
include_once( dirname(__FILE__) . '/classes/core/Authorization.php');
include_once( dirname(__FILE__) . '/classes/core/User.php');
include_once( dirname(__FILE__) . '/classes/core/Member.php');
include_once( dirname(__FILE__) . '/classes/core/Guest.php');
include_once( dirname(__FILE__) . '/classes/core/Exceptions.php');
include_once( dirname(__FILE__) . '/classes/core/PageError.php');
include_once( dirname(__FILE__) . '/classes/core/Language.php');

// include Pear root and set include path for packages
set_include_path( dirname(__FILE__) . '/vendor/pear/' . PATH_SEPARATOR . get_include_path());
include_once( dirname(__FILE__) . '/vendor/pear/PEAR.php');

// include Pear packages Translation2, Auth, MDB2
include_once( dirname(__FILE__) . '/vendor/pear/Translation2.php');
include_once( dirname(__FILE__) . '/vendor/pear/Translation2/Admin.php');
include_once( dirname(__FILE__) . '/vendor/pear/Auth.php');
include_once( dirname(__FILE__) . '/vendor/pear/MDB2.php');

// set include path to reference Pear files

// start session
session_start();

// Get the translator
$translator =& Translation2::factory($driver, $dbinfo, $params);
global $translator;

// start up router
$router = new Router;

//Get an instance of Dispatcher
$dispatcher = new Dispatcher;
$dispatcher->setSuffix('Controller');
$dispatcher->setClassPath( dirname(__FILE__) . '/classes/controller/');

// load routes
require_once( dirname(__FILE__) . "/routes.inc.php" );

$url = urldecode($_SERVER['REQUEST_URI']);

try {
    $found_route = $router->findRoute($url);
	
	$user = Authentication::getCurrentUser();
	if( !$user->isAuthorized( $found_route ) )
	{
		throw new NotAuthorizedException("Access denied");
	}
	
    $dispatcher->dispatch( $found_route );
} catch ( NotAuthorizedException $e ) {
	$userController = new userController();
	$a = new Auth("MDB2", $auth_options, array($userController, "login"), true);
	$a->start();
} catch ( RouteNotFoundException $e ) {
    PageError::show('404', $url);
} catch ( badClassNameException $e ) {
    PageError::show('400', $url);
} catch ( classFileNotFoundException $e ) {
    PageError::show('500', $url);
} catch ( classNameNotFoundException $e ) {
    PageError::show('500', $url);
} catch ( classMethodNotFoundException $e ) {
    PageError::show('500', $url);
} catch ( classNotSpecifiedException $e ) {
    PageError::show('500', $url);
} catch ( methodNotSpecifiedException $e ) {
    PageError::show('500', $url);
} catch ( BlogException $e ) {
	PageError::show('500', $e->getMessage());
}

?>